package cn.easytogo.service.impl;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import cn.easytogo.service.IBaseDAO;

public class BaseDAOImpl  implements IBaseDAO{
	private String resource ="SqlMapConfig.xml";
	private SqlMapClient sqlMap;
	
	
	public BaseDAOImpl() {
		super();
		try {
			Reader reader = Resources.getResourceAsReader(resource);
			sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getResource() {
		return resource;
	}

	public void setResource(String resource) {
		this.resource = resource;
	}

	public SqlMapClient getSqlMap() {
		return sqlMap;
	}

	public void setSqlMap(SqlMapClient sqlMap) {
		this.sqlMap = sqlMap;
	}

	public List queryForList(String sql, Object obj) throws SQLException {
		return sqlMap.queryForList(sql, obj);
	}

	public int delete(String s, Object obj) throws SQLException {
		return sqlMap.delete(s,obj);
	}

	public int delete(String s) throws SQLException {
		return sqlMap.delete(s);
	}

	public Object insert(String s, Object obj) throws SQLException {
		Object o = sqlMap.insert(s, obj);
		return o;
	}

	public Object insert(String s) throws SQLException {
		return sqlMap.insert(s);
	}

	public List queryForList(String s) throws SQLException {
		return sqlMap.queryForList(s);
	}

	public List queryForList(String s, Object obj, int i, int j)
			throws SQLException {
		return sqlMap.queryForList(s,obj,i, j);
	}

	public List queryForList(String s, int i, int j) throws SQLException {
		return sqlMap.queryForList(s,i, j);
	}

	public Map queryForMap(String s, Object obj) throws SQLException {
		List list = sqlMap.queryForList(s, obj);
		if(list!=null&&list.size()>0){
			return (Map) list.get(0);
		}
		return null;
	}
	
	public Map queryForMap(String s, Object obj, String s1) throws SQLException {
		return sqlMap.queryForMap(s,obj,s1);
	}

	public Map queryForMap(String s, Object obj, String s1, String s2)
			throws SQLException {
		return sqlMap.queryForMap(s,obj,s1,s2);
	}

	public Object queryForObject(String s, Object obj) throws SQLException {
		return sqlMap.queryForObject(s,obj);
	}

	public Object queryForObject(String s) throws SQLException {
		return sqlMap.queryForObject(s);
	}

	public Object queryForObject(String s, Object obj, Object obj1)
			throws SQLException {
		return sqlMap.queryForObject(s,obj,obj1);
	}

	public int update(String s, Object obj) throws SQLException {
		return sqlMap.update(s,obj);
	}

	public int update(String s) throws SQLException {
		return sqlMap.update(s);
	}

	public void commitTransaction() throws SQLException {
		sqlMap.commitTransaction();
	}

	public void endTransaction() throws SQLException {
		sqlMap.endTransaction();
	}

	public void startTransaction() throws SQLException {
		sqlMap.startTransaction();
	}

}
